package utils

import (
	"crypto/md5"
	"encoding/hex"
	"sort"
	"strings"
)

// AppendSignCode 添加请求参数签名
func AppendSignCode(signKey string, params map[string]string) string {
	//按key排序
	var keys sort.StringSlice
	for k := range params {
		keys = append(keys, k)
	}
	sort.Sort(keys)

	//拼接参数字符串
	s := ""
	for _, key := range keys {
		s += key         //key
		s += params[key] //value
	}
	s = signKey + s
	s = s + signKey
	//转大写
	s = strings.ToUpper(s)
	//md5
	return textToMD5L32(s)
}

//文本转成md5
func textToMD5L32(str string) string {
	md5Ctx := md5.New()
	md5Ctx.Write([]byte(str))
	sum := md5Ctx.Sum(nil)
	return hex.EncodeToString(sum)
}
